#include"queue.h"
typedef struct mystack
{
	queue q1;
	queue q2;
}mystack,*pmystack;
pmystack mystackcreate()
{
	pmystack pst = (pmystack)malloc(sizeof(mystack));
	queueinit(&pst->q1);
	queueinit(&pst->q2);
	return pst;
}
void mystackpush(pmystack obj, int x)
{
	if (!queueempty(&obj->q1))
	{
		queuepush(&obj->q1, x);
	}
	else
	{
		queuepush(&obj->q2, x);
	}
}
int mystackpop(pmystack obj)
{
	queue* empty = &obj->q1;
	queue* nonempty = &obj->q2;
	if (!queueempty(&obj->q1))
	{
		nonempty = &obj->q1;
		empty = &obj->q2;
	}
	while (queuesize(nonempty) > 1)
	{
		queuepush(empty, queuefront(nonempty));
		queuepop(nonempty);
	}
	int top = queuefront(nonempty);
	queuepop(nonempty);
	return top;
}
int mystacktop(pmystack obj)
{
	if (!queueempty(&obj->q1))
	{
		return queueback(&obj->q1);
	}
	else
	{
		return queueback(&obj->q2);
	}
}
bool mystackempty(pmystack obj)
{
	return queueempty(&obj->q1) && queueempty(&obj->q2);
}
void mystackfree(pmystack obj)
{
	queuedestroy(&obj->q1);
	queuedestroy(&obj->q2);
	free(obj);
}